{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## time库基础知识和实例演示\n",
    "> **1.Time库的作用**\n",
    "> **2\\. Time库的使用**\n",
    "> **3.实例**\n",
    "\n",
    "**1.Time库的作用**\n",
    "\n",
    "-   time库是Python中处理时间的**标准库**\n",
    "-   提供**获取系统时间**并**格式化**输出功能\n",
    "-   提供系统级精确计时功能，用于**程序性能分析**\n",
    "\n",
    "**2\\. Time库的使用**\n",
    "\n",
    "先明确几个概念：\n",
    "\n",
    "-   **时间戳：**格林威治时间1970年01月01日00分00秒（北京时间1970年01月01日08时00分00秒）起至现在的**总秒数，是个数字**。\n",
    "-   Python中获取时间的常用方法是，先得到时间戳，再将其转换成想要的时间格式。\n",
    "-   **元组struct\\_time**: 日期、时间是包含许多变量的，所以在Python中定义了一个元组struct\\_time将所有这些变量组合在一起，包括：年、月、日、小时、分钟、秒等。\n",
    "\n",
    "**1）时间获取函数**\n",
    "\n",
    "![](https://pic4.zhimg.com/v2-cdd264f0b2a195067ae3906f10b88cf7_b.jpg)\n",
    "\n",
    "![](https://pic2.zhimg.com/v2-745dc8488a86881536b27c49c7e214d1_b.jpg)\n",
    "\n",
    "![](https://pic4.zhimg.com/v2-99f34a81f87a9c83b272f2048203608b_b.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "\n",
    "**2）时间格式化**:将时间以合理的方式展示出来\n",
    "\n",
    "![](https://pic1.zhimg.com/v2-2570572e5adac5082c061a698e5a1e28_b.jpg)\n",
    "\n",
    "![](https://pic4.zhimg.com/v2-e4cc9f56057cb3f31dc437deb6d4b5af_b.jpg)\n",
    "\n",
    "![](https://pic2.zhimg.com/v2-06933635ff8320af4d39334128cf74a9_b.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "\n",
    "**3）问题**:我们是否可以以字符串的形式构造一个时间，如”2018-01-26 12:55:20”,然后将其变成一个时间变量呢？\n",
    "\n",
    "答案是可以的，通过展示模板定义的参数逐一解析字符串中对应的每一个值，它可能会形成一个时间变量。转化成一个计算机内部可以操作的一个时间。\n",
    "\n",
    "![](https://pic1.zhimg.com/v2-1b391fb6d55836cd3e5a8d28b4018d5c_b.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "\n",
    "**4）程序计时应用**:测量起止动作所经历时间的过程\n",
    "\n",
    "![](https://pic1.zhimg.com/v2-e96f367afdbecc60895202f0f19d3a38_b.jpg)\n",
    "\n",
    "![](https://pic2.zhimg.com/v2-22b1e1b92a887be8dea2214991c0a6bd_b.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "\n",
    "-   **疑问**:perf\\_counter()函数是用来做程序计时，但是time()函数不是也可以吗？\n",
    "\n",
    "> 解答：\n",
    "> time()精度上相对没有那么高，而且受系统的影响，适合表示日期时间或者大程序程序的计时。\n",
    "> perf\\_counter()适合小一点的程序测试，会计算sleep()时间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "\n",
    "**3.实例：文本进度条**\n",
    "\n",
    "**1）实例1**：每次进度换行:\n",
    "\n",
    "```\n",
    "print()函数默认输出一个字符后换到下一行，所以不用进行其他操作\n",
    "```\n",
    "\n",
    "![](https://pic3.zhimg.com/v2-370fa4192428d5b5afa949d402fe86e2_b.jpg)\n",
    "\n",
    "> 输出结果>>\n",
    "\n",
    "![](https://pic1.zhimg.com/v2-b4e883a73b6f5e2931f4bd6f7935dd18_b.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "\n",
    "**2）实例2**：每次进度不换行，只是不断地进行刷新：用后打印的字符覆盖之前的字符\n",
    "\n",
    "-   为了实现**单行动态刷新**，就需要要求我们的程序在输出某一个字符的字符串的时候，**不能够换行到下一行**。因为换到下一行后，之前的信息不能够被修改\n",
    "-   转义符 **\\\\r（光标移动到本行首）**\n",
    "-   有关转义符的使用当时困扰了我很久，比如应该放在哪个位置，所以单独放在了一个文档里专门介绍啦~~\n",
    "\n",
    "![](https://pic2.zhimg.com/v2-48e5fede3ed9e62e0d2fd7a6e5efd771_b.jpg)\n",
    "\n",
    "> 输出结果>>\n",
    "\n",
    "![](https://pic1.zhimg.com/v2-1b89f7d630f60e4e8d806438794af658_b.jpg)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}